﻿using BC.DataLayer;
using BC.Entities;
using BC.IoC;
using BC.Utilities;
using BM.DataLayer.User.BusinessEntities;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BM.DataLayer.User
{
    public class AddUserDL
    {

        public ReplyValueBase addUser(AddUserBE addUserBE)
        {
            ReplyValueBase rv = new ReplyValueBase();
            try
            {
                List<SqlParameter> sqlParameters = new List<SqlParameter>();

                ISqlHelper sqlHelper = InstanceManager.Instance.Resolve<ISqlHelper>();

                sqlParameters.Add(ParameterHelper.CreateSqlParameter("@user", addUserBE.Username));
                sqlParameters.Add(ParameterHelper.CreateSqlParameter("@password", addUserBE.Password));
                sqlParameters.Add(ParameterHelper.CreateSqlParameter("@email", addUserBE.Email));
                sqlParameters.Add(ParameterHelper.CreateSqlParameter("@join_date", DateTime.Now));

                sqlHelper.ExecuteNonQuery(getSqlRequest(), sqlParameters);

            }
            catch (Exception e)
            {
                ErrorPublisher.Publish(e);
                rv.ReturnCode = BC.Entities.ReplyValueBase.ReturnCodes.SqlError;
            }

            return rv;
        }
 
        private string getSqlRequest()
        {
            string sqlRequest = "INSERT INTO USER " +
                                    "USER, " +
                                    "PASSWORD, " +
                                    "EMAIL, " +
                                    "JOIN_DATE " +
                                "VALUES(" +
                                    "@user, " +
                                    "@password, " + 
                                    "@email, " + 
                                    "@join_date)";

            return sqlRequest;
        }
    }

    
}
